var ns, toast;
var app = new Vue({
    el: '#app',
    data: {
        headerlist: [],
        ProjectIDs: '',
        RoomIDs: '',
        RoomList: [],
        FullName: '',
        MeterCategoryID: 0,
        MeterCategoryDesc: '全部',
        form: {
            keywords: '',
            writeDate: '',
            pageindex: 0,
            pagesize: 10,
        },
        PointStatus: 0,
        current_item_length: 0,
        scroll_top: 0,
        can_scroll: true,
        is_searching: false,
        searchbar_click: false,
        MeterList: [
            { ID: 0, Name: '全部' },
            { ID: 2, Name: '电表' },
            { ID: 1, Name: '水表' },
            { ID: 3, Name: '气表' },
        ],
        writeDateDay: 0,
    },
    methods: {
        get_data: function () {
            var that = this;
            if (that.searchbar_click) {
                that.searchbar_click = false;
                if (!that.form.keywords) {
                    api.toast({
                        msg: '请输入关键字',
                        duration: 2000,
                        location: 'bottom'
                    });
                    return;
                }
            }
            that.is_searching = true;
            that.can_scroll = false;
            var options = {};
            options.action = "getprivatemeterroomlist";
            options.ProjectIDs = that.ProjectIDs;
            options.RoomIDs = that.RoomIDs;
            options.pagesize = that.form.pagesize;
            options.pageindex = that.form.pageindex;
            options.MeterCategoryID = that.MeterCategoryID;
            options.keywords = that.form.keywords;
            options.WriteDate = that.form.writeDate;
            console.log(JSON.stringify(options));
            ns.post(options, function (succeed, data, err) {
                toast.hide();
                console.log(JSON.stringify(data));
                if (succeed) {
                    if (data.list.length == that.form.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.form.pageindex == 0) {
                        that.RoomList = data.list;
                    } else {
                        that.RoomList = that.RoomList.concat(data.list);
                    }
                    app.current_item_length = that.RoomList.length;
                } else if (err) {
                    that.RoomList = [];
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        do_save: function (item) {
            var that = this;
            if (item.EndPoint < item.StartPoint) {
                api.toast({
                    msg: '抄表读数不能小于起始读数',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            var options = {};
            options.action = "savemeterwritepoint";
            options.item = JSON.stringify(item);
            ns.post(options, function (succeed, data, err) {
                console.log(JSON.stringify(options));
                if (succeed) {
                    api.toast({
                        msg: '保存成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    item.TotalPoint = item.EndPoint - item.StartPoint;
                    // that.get_data();
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        do_reload: function () {
            var that = this;
            that.form.pageindex = 0;
            that.get_data();
        },
        do_select_status: function () {
            var that = this;
            /*            that.MeterCategoryID = status;*/
            that.do_reload();
        },
        do_select_time: function () {
            var that = this;
            var currentTime = that.form.writeDate;
            api.openPicker({
                type: 'date',
                date: currentTime + '-' + that.writeDateDay,
                title: '选择日期'
            }, function (ret, err) {
                if (ret) {
                    var year = ret.year;
                    var month = (ret.month >= 10 ? ret.month : '0' + ret.month);
                    that.writeDateDay = (ret.day >= 10 ? ret.day : '0' + ret.day);
                    currentTime = year + '-' + month;
                    that.form.writeDate = currentTime;
                    that.do_reload();
                }
            });
        },
        do_search: function () {
            var that = this;
            that.searchbar_click = true;
            that.do_reload();
        },
        do_select_project: function () {
            var that = this;
            ns.openWin('chooseproject_frm', '选择项目', {
                selecttype: 2
            });
        },
        get_select_project: function (data) {
            var that = this;
            that.ProjectIDs = data.projectids;
            that.RoomIDs = data.roomids;
            that.FullName = data.name;
            that.headerlist = eval('(' + data.headerlist + ')')
            setTimeout(function () {
                that.do_reload();
                var top_h = $api.offset($api.byId('topheadercontent')).h
                $api.css($api.byId('topheader'), 'height:' + top_h + 'px');
                $api.css($api.byId('main_refresh'), 'margin-top:' + top_h+ 'px');
            }, 500);
        },
        getSpanCss: function (item) {
            return item.PointStatus == 0 ? 'redcss' : 'greencss'
        },
        clear_searchVal: function () {
            var that = this;
            document.getElementById("search-input").value = '';
        },
        do_dell: function (phonenumber) {
            api.call({
                type: 'tel_prompt',
                number: phonenumber
            });
        },
        init_FullName: function () {
            var that = this;
            if (that.FullName == '') {
                that.FullName = '请选择资源信息';
            }
        },
        do_open_header: function (item) {
            var that = this;
            if (!item.isParent) {
                return;
            }
            ns.openWin('chooseproject_frm', '选择项目', {
                parentid: item.id,
                selecttype: 2
            });
        },
        initPicker: function () {
            var _this = this;
            (function ($, doc) {
                $.init();
                $.ready(function () {
                    // 选择仪表类型
                    var pickerMeter = new $.PopPicker({
                        layer: 1,
                        fileID: 'ID',
                        fileName: 'Name',
                    });
                    var meterData = _this.MeterList || [];
                    pickerMeter.setData(meterData);
                    var showMeterPickerButton = doc.getElementById('choosemeter');
                    showMeterPickerButton.addEventListener('click', function (event) {
                        pickerMeter.show(function (items) {
                            _this.MeterCategoryID = items[0].value;
                            _this.MeterCategoryDesc = items[0].text;
                            _this.do_reload();
                            //返回 false 可以阻止选择框的关闭
                            //return false;
                        });
                    }, false);

                    //选择日期
                    var btn = document.querySelector('#choosedate');
                    btn.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            _self.picker.show(function (rs) {
                                _this.form.writeDate = rs.value;
                                _this.do_reload();
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.form.writeDate = rs.value;
                                    _this.do_reload();

                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);

                });
            })(mui, document);
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    //初始化时间
    var data = new Date();
    var year = data.getFullYear();
    var month = data.getMonth() + 1;
    app.form.writeDate = year + '-' + (month >= 10 ? month : '0' + month);
    app.writeDateDay = data.getDay() + 1;
    app.initPicker();

    app.type = api.pageParam.type || 0;
    toast = new auiToast();
    app.get_select_project(api.pageParam);
    api.addEventListener({
        name: 'do_choose_project_complete'
    }, function (ret) {
        app.get_select_project(ret.value);
    })
    app.init_FullName();
    ns.init_search();
    ns.init_pull();
}
